# conding:utf-8

'''
##获取 冯提莫.json 中的关键值:

GET http://trackercdnbj.kugou.com/i/v2/?album_audio_id=67409789&behavior=play&cmd=25&album_id=14015862&hash=f87a42cc53b022e313bb08c4bec3a848&userid=774289113&pid=2&version=9225&area_code=1&appid=1005&key=d01f460dde313fc0193a9b6e75663b55&pidversion=3001&with_res_tag=1 HTTP/1.1
User-Agent: Android800-AndroidPhone-9225-130-0-NetMusic-wifi
KG-THash: 18a6835
Accept-Encoding: gzip, deflate
KG-RC: 1
Host: trackercdnbj.kugou.com
Connection: Keep-Alive

## 返回 json 数据 :

<!--KG_TAG_RES_START-->{
  "fileHead": 100,
  "q": 0,
  "fileSize": 3754633,
  "fileName": "\u51af\u63d0\u83ab - \u6765\u81ea\u5929\u5802\u7684\u9b54\u9b3c",
  "status": 1,
  "url": [
    "http:\/\/fs.android2.kugou.com\/e7ec26f820accc2c33c44410dfe6bb40\/5cf54abd\/G096\/M01\/02\/13\/oA0DAFkZxy-APBVUADlKicjzgMg989.mp3",
    "http:\/\/fs.mobile.kugou.com\/201906040028\/ae621518de6557472abc511a0f3493de\/G096\/M01\/02\/13\/oA0DAFkZxy-APBVUADlKicjzgMg989.mp3"
  ],
  "extName": "mp3",
  "bitRate": 128000,
  "timeLength": 235
}<!--KG_TAG_RES_END-->


## 提取 url 中的MP3链接, 并下载

GET http://fs.android2.kugou.com/e7ec26f820accc2c33c44410dfe6bb40/5cf54abd/G096/M01/02/13/oA0DAFkZxy-APBVUADlKicjzgMg989.mp3 HTTP/1.1
Connection: keep-alive
Content-Type: */*
Content-Length: 0
Host: fs.android2.kugou.com
User-Agent: AndroidPhone-9225-DownloadRangeNotBlock-wifi


'''

url = 'http://trackercdnbj.kugou.com/i/v2/?' \
      'album_audio_id=67409789&' \
      'behavior=play&' \
      'cmd=25&' \
      'album_id=14015862&' \
      'hash=f87a42cc53b022e313bb08c4bec3a848&' \
      'userid=774289113&' \
      'pid=2&' \
      'version=9225&' \
      'area_code=1&' \
      'appid=1005&' \
      'key=d01f460dde313fc0193a9b6e75663b55&' \
      'pidversion=3001&' \
      'with_res_tag=1 HTTP/1.1'


import json
import requests

f = open('ftm.json', 'r')
info = json.load(f)
f.close()

info = info['data']['info']

baseUrl = 'http://trackercdnbj.kugou.com/i/v2/?' \
      'album_audio_id=%s&' \
      'behavior=play&' \
      'cmd=25&' \
      'album_id=%s&' \
      'hash=%s&' \
      'key=d01f460dde313fc0193a9b6e75663b55&' \
      'pidversion=3001&' \
      'with_res_tag=1'
      # 'userid=774289113&' \

for i in info[:3]:
    print(i.get('album_audio_id'))
    print(i.get('album_id'))
    print(i.get('hash'))
    print(i.get('filename'))
    print(i.get('extname'))
    # print(i.get('singer_name'))
    # print(i.get('album_name'))
    # print(i.get('remark'))

    album_audio_id = str(i.get('album_audio_id'))
    album_id = i.get('album_id')
    hash = i.get('hash')

    url = baseUrl % (album_audio_id, album_id, hash)
    print(url)
    # url = 'http://trackercdnbj.kugou.com/i/v2/?album_audio_id=67409789&behavior=play&cmd=25&album_id=14015862&hash=f87a42cc53b022e313bb08c4bec3a848&userid=774289113&pid=2&version=9225&area_code=1&appid=1005&key=d01f460dde313fc0193a9b6e75663b55&pidversion=3001&with_res_tag=1'
    # headers = {'User-Agent':'Android800-AndroidPhone-9225-130-0-NetMusic-wifi'}
    # con = requests.get(url=url, headers=headers)
    # print(con.content)

    print('='*23)

url = 'http://trackercdnbj.kugou.com/i/v2/?album_audio_id=67409789&behavior=play&cmd=25&album_id=14015862&hash=f87a42cc53b022e313bb08c4bec3a848&userid=774289113&pid=2&version=9225&area_code=1&appid=1005&key=d01f460dde313fc0193a9b6e75663b55&pidversion=3001&with_res_tag=1'
url = 'http://trackercdnbj.kugou.com/i/v2/?album_audio_id=109448450&behavior=play&cmd=25&album_id=0&hash=E14F443549692F1B15F6B5EDB95082DC&key=d01f460dde313fc0193a9b6e75663b55&pidversion=3001&with_res_tag=1'
headers = {'User-Agent':'Android800-AndroidPhone-9225-130-0-NetMusic-wifi'}
con = requests.get(url=url, headers=headers)
print(con.content)